package com.ferrlo.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ferrlo.model.Project;
import com.ferrlo.service.IProjectManager;

@Controller
public class SearchController {
	
	@Resource(name="projectManager")
	private IProjectManager projectManager;
	
	@RequestMapping(value="/search")
	public String search(Map<String, Object>map,HttpServletRequest request){
		
		String content = request.getParameter("content");
		List<Project> projects = searchByLike(content);
		
		map.put("projects", projects);

		return "index";
	}
	
	@RequestMapping(value="/search.json")
	public Map<String, Object> search_JSON(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		
		String content = request.getParameter("content");
		List<Project> projects = searchByLike(content);
		map.put("projects", projects);
		
		return map;
	}
	
	
	private List<Project> searchByLike(String content){
		
		String dqQuery = "ProjectManager.findUserByContent.query";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("content", content);
		List<Project> projects = projectManager.findListByDynamicQuery(dqQuery, param);
		
		return projects;
	}

}
